<template>
	<view class="content" >
		<view style="height: 100rpx;"></view>
		<view style="width: 90%;position: fixed;top: 100rpx;">
			<uni-icons @click="back" type="back" style="color: #000;" size="30"></uni-icons>
		</view>
		<view style="height: 100rpx;"></view>

		<image style="width: 150rpx;height: 150rpx;border-radius: 50%;margin-top: 80rpx;" src="/static/logo.png"
			mode="aspectFill"></image>

		<view style="font-size: 35rpx;font-weight: bold;margin-top: 40rpx;color: #000;">{{id == 0 ? '修改手机号账号密码' : '修改邮箱账号密码'}}
		</view>
		<!-- 		<view style="font-size: 25rpx;color: #999999;">未注册的手机号, 将为您自动注册</view> -->

		<view
			style="width: 80%;display: flex;justify-content: center;background-color: rgba(225, 225, 225, .4);border-radius: 50rpx;margin-top: 40rpx;">
			<input v-model="form.user" style="width: 90%;padding: 30rpx 0;color: #000;" :placeholder="id==0 ? '请输入手机号' : '请输入邮箱'"
				placeholder-class="pcls" />
		</view>
		<view style="width: 80%;border-radius: 50rpx;background-color:  rgba(225, 225, 225, .4);display: flex;align-items: center;color: #000;margin-top: 20rpx;justify-content: center;">
			<view style="width: 90%;display: flex;align-items: center;">
				<input v-model="form.pass" :password="showPassword" :placeholder="$t('login.3')" placeholder-class="pcls"
					style="flex: 1;padding: 30rpx 0rpx;" />
						
				<view v-if="form.pass" style="margin-right: 10rpx;">
					<uni-icons @click="showPassword = false" v-if="showPassword" type="eye" size="25"
						style="font-size: 40rpx;color: #000;"></uni-icons>
					<uni-icons @click="showPassword = true" v-else type="eye-filled" style="color: #000;font-size: 40rpx"
						size="25"></uni-icons>
				</view>
			</view>
		
		</view>
		<view
			style="width: 80%;border-radius: 10rpx;background-color: rgba(225, 225, 225, .4);display: flex;align-items: center;border-radius: 50rpx;margin-top: 20rpx">
			<input :placeholder="$t('login.14')" v-model="form.code" placeholder-class="pcls"
				style="padding: 30rpx 20rpx;width: 90%;color: #000;" />
			<view class="puBut3" @click="send" v-if="codeFalg"
				style="padding: 10rpx;width:150rpx;text-align: center;margin-right: 20rpx;border-radius: 50rpx;font-size: 25rpx;">
				{{$t('login.15')}}
			</view>
			<view class="puBut9" v-else
				style="padding: 10rpx;width:150rpx;text-align: center;margin-right: 20rpx; border-radius: 50rpx;font-size: 25rpx;">
				{{countDown}}S
			</view>
		</view>

		<view @click="updatePass" style="width: 80%;margin-top: 80rpx;">
			<button class="puBut3" style="width: 100%;font-size: 30rpx;border-radius: 40rpx;color: #fff;">修改</button>
		</view>

	</view>
</template>

<script>
	import {
		encryption
	} from "@/static/js/aesEncrypt.js"
	import methods from "@/utils/methods.js"
	export default {
		data() {
			return {
				id: 0,
				country: '',
				showPassword: true,
				form: {
					user: '',
					code: '',
					pass: ''
				},
				radioFalg: false,
				codeFalg: true,
				countDown: 60,
			}
		},
		onLoad(data) {
			if (data.id) {
				this.id = data.id
			}
			let phone = ''
			if (this.id == 0) {
				phone = uni.getStorageSync('phone');
			} else {
				phone = uni.getStorageSync('email');
			}

			if (phone) {
				this.form.user = phone
			}
		},
		watch: {
			countDown(newVal, oldVal) {
				if (newVal == 0) {
					clearInterval(this.codeDsObj)
					this.codeDsObj = null
					this.codeFalg = true
				}
			}
		},
		mounted() {

		},
		methods: {
			updatePass() {
				let self = this
				if (!self.form.pass) {
					uni.showToast({
						icon: 'none',
						title: self.$t('login.17')
					})
					return
				}
				if (!self.form.code) {
					uni.showToast({
						icon: 'none',
						title: self.$t('login.14')
					})
					return
				}
				if (self.id == 0) {
					if (!self.form.user) {
						uni.showToast({
							icon: 'none',
							title: self.$t('common.30')
						})
						return false
					} else {
						var pattern = /^1[03456789]\d{9}$/;
						let falg = pattern.test(self.form.user);
						if (!falg) {
							uni.showToast({
								icon: 'none',
								title: self.$t('common.31')
							})
							return false
						}
					}
					methods.getEncryptedString().then(res => {
						if (res.data.code == 200) {
							if (res.data.data) {
								//加密用户手机号
								const maxUserIphone = encryption(self.form.user, res.data.data.key, res.data.data
									.iv);
								//加密用户密码
								const maxPassword = encryption(self.form.pass, res.data.data.key, res.data.data
								.iv);
								let obj = {
									phone: maxUserIphone,
									code: self.form.code,
									password: maxPassword
								}
								methods.forgetPwd(obj).then(res => {
									if (res.data.code == 200) {
										self.$store.state.isSx = true
										uni.setStorageSync('phone', self.form.user)
										uni.setStorageSync('pass', self.form.pass)
										uni.showToast({
											icon: 'none',
											title: res.data.message
										})

										setTimeout(() => {
											uni.navigateBack()
										}, 1500)
									}
								}).catch(err => {
									uni.showToast({
										icon: 'none',
										title: err.data.message
									})
								})
							}
						}
					})
				} else {
					if (!self.form.user) {
						uni.showToast({
							icon: 'none',
							title: self.$t('common.40')
						})
						return false
					} else {
						let re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
						let falg = re.test(self.form.user);
						if (!falg) {
							uni.showToast({
								icon: 'none',
								title: self.$t('common.39')
							})
							return false
						}
					}
					methods.getEncryptedString().then(res => {
						if (res.data.code == 200) {
							if (res.data.data) {
								//加密用户密码
								const maxPassword = encryption(self.form.pass, res.data.data.key, res.data.data
								.iv);
								let obj = {
									email: self.form.user,
									code: self.form.code,
									password: maxPassword
								}
								methods.forgetPwdByEmail(obj).then(res => {
									if (res.data.code == 200) {
										self.$store.state.isSx = true
										uni.setStorageSync('phone', self.form.user)
										uni.setStorageSync('pass', self.form.pass)
										uni.showToast({
											icon: 'none',
											title: res.data.message
										})

										setTimeout(() => {
											uni.navigateBack()
										}, 1500)
									}
								}).catch(err => {
									uni.showToast({
										icon: 'none',
										title: err.data.message
									})
								})
							}
						}
					})
				}
			},
			send() {
				let self = this
				if (!self.form.pass) {
					uni.showToast({
						icon: 'none',
						title: self.$t('login.17')
					})
					return
				}

				if (self.id == 0) {
					if (!self.form.user) {
						uni.showToast({
							icon: 'none',
							title: self.$t('common.30')
						})
						return false
					} else {
						var pattern = /^1[03456789]\d{9}$/;
						let falg = pattern.test(self.form.user);
						if (!falg) {
							uni.showToast({
								icon: 'none',
								title: self.$t('common.31')
							})
							return false
						}
					}
					methods.getEncryptedString().then(res => {
						if (res.data.code == 200) {
							if (res.data.data) {
								//加密用户手机号
								const maxUserIphone = encryption(self.form.user, res.data.data.key, res.data.data
									.iv);
								//加密用户密码
								const maxPassword = encryption(self.form.pass, res.data.data.key, res.data.data
									.iv);
								let obj = {
									phone: maxUserIphone,
									code: self.form.code,
									password: maxPassword
								}
								methods.forgetPwd(obj).then(res => {
									if (res.data.code == 100) {
										self.$store.state.isSx = true
										self.countDown = 60
										self.codeFalg = false
										self.codeDsObj = setInterval(() => {
											self.countDown -= 1
										}, 1000)

										uni.showToast({
											icon: 'none',
											title: res.data.message
										})
									}
								})
							}
						}
					})
				} else {
					if (!self.form.user) {
						uni.showToast({
							icon: 'none',
							title: self.$t('common.40')
						})
						return false
					} else {
						let re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
						let falg = re.test(self.form.user);
						if (!falg) {
							uni.showToast({
								icon: 'none',
								title: self.$t('common.39')
							})
							return false
						}
					}

				}
				methods.getEncryptedString().then(res => {
					if (res.data.code == 200) {
						if (res.data.data) {
							//加密用户密码
							const maxPassword = encryption(self.form.pass, res.data.data.key, res.data.data.iv);
							let obj = {
								email: self.form.user,
								code: '',
								password: maxPassword
							}
							methods.forgetPwdByEmail(obj).then(res => {
								if (res.data.code == 100) {
									self.$store.state.isSx = true
									self.countDown = 60
									self.codeFalg = false
									self.codeDsObj = setInterval(() => {
										self.countDown -= 1
									}, 1000)

									uni.showToast({
										icon: 'none',
										title: res.data.message
									})
								}
							})
						}
					}
				})

			},
			back() {
				uni.navigateBack()
			}
		}
	}
</script>

<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		min-height: 100vh;
		background: linear-gradient(to bottom, #DFD7F9 0px, #f2f0f9 398rpx, #F5F6F8 100%)
	}

	.pcls {
		font-size: 25rpx;
		color: #000;
	}
</style>